package com.llsydn.beijingnews.fragment;

import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.RadioGroup;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.llsydn.beijingnews.R;
import com.llsydn.beijingnews.activity.MainActivity;
import com.llsydn.beijingnews.adapter.ContentFragmentAdapter;
import com.llsydn.beijingnews.base.BaseFragment;
import com.llsydn.beijingnews.base.BasePager;
import com.llsydn.beijingnews.pager.*;
import com.llsydn.beijingnews.util.LogUtil;
import com.llsydn.beijingnews.view.NoScrollViewPager;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;

/**
 * @author lilinshen
 * @title 主页面的fragment
 * @description 请填写相关描述
 * @date 2018/10/25 9:14
 */
public class ContentFragment extends BaseFragment {
    // 2.初始化控件
    @ViewInject(R.id.viewpager)
    private NoScrollViewPager viewPager;
    @ViewInject(R.id.rg_main)
    private RadioGroup rg_main;
    // 装5个页面的集合
    private ArrayList<BasePager> basePagers;

    @Override
    public View initView() {
        LogUtil.e("正文Fragment被初始化了");
        View view = View.inflate(context, R.layout.content_fragment, null);
        // 1.把视图注入到框架中，让contentfragmen和view关联
        x.view().inject(ContentFragment.this, view);
        return view;
    }

    @Override
    public void initData() {
        super.initData();
        LogUtil.e("正文Fragment数据被初始化了");
        // 初始化5个页面，并且放入集合中
        basePagers = new ArrayList<BasePager>();
        basePagers.add(new HomePager(context));
        basePagers.add(new NewsPager(context));
        basePagers.add(new SmartPager(context));
        basePagers.add(new GovaffairPager(context));
        basePagers.add(new SettingPager(context));

        // 默认选中首页
        rg_main.check(R.id.rb_home);
        // 设置radiogroup监听
        rg_main.setOnCheckedChangeListener(new MyCheckedChangeListener());

        // 设置viewpager的适配器
        viewPager.setAdapter(new ContentFragmentAdapter(basePagers));

        // 监听某个页面被选中，初始化对应页面的数据
        viewPager.addOnPageChangeListener(new MyOnPageChangeListener());
        basePagers.get(0).initData();
        isEnableSlidingMenu(SlidingMenu.TOUCHMODE_NONE);
    }

    /**
     * 得到新闻中心
     *
     * @param
     * @return
     */
    public NewsPager getNewsPager() {
        return (NewsPager) basePagers.get(1);
    }

    /**
     * viewpager页面被选中监听
     */
    class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int i, float v, int i1) {

        }

        /**
         * 当页面被选中时回调这个方法
         *
         * @param i
         */
        @Override
        public void onPageSelected(int i) {
            // 调用被选中页面的initData()方法
            basePagers.get(i).initData();
        }

        @Override
        public void onPageScrollStateChanged(int i) {

        }
    }

    /**
     * radiogroup监听
     */
    class MyCheckedChangeListener implements RadioGroup.OnCheckedChangeListener {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId) {
                case R.id.rb_home:
                    viewPager.setCurrentItem(0, false);
                    isEnableSlidingMenu(SlidingMenu.TOUCHMODE_NONE);
                    break;
                case R.id.rb_news:
                    viewPager.setCurrentItem(1, false);
                    isEnableSlidingMenu(SlidingMenu.TOUCHMODE_FULLSCREEN);
                    break;
                case R.id.rb_smart:
                    viewPager.setCurrentItem(2, false);
                    isEnableSlidingMenu(SlidingMenu.TOUCHMODE_NONE);
                    break;
                case R.id.rb_govaffair:
                    viewPager.setCurrentItem(3, false);
                    isEnableSlidingMenu(SlidingMenu.TOUCHMODE_NONE);
                    break;
                case R.id.rb_settting:
                    viewPager.setCurrentItem(4, false);
                    isEnableSlidingMenu(SlidingMenu.TOUCHMODE_NONE);
                    break;
            }
        }

    }

    /**
     * 根据传入的参数是否让slidingmenu可以滑动
     *
     * @param mode
     */
    private void isEnableSlidingMenu(int mode) {
        MainActivity mainActivity = (MainActivity) context;
        mainActivity.getSlidingMenu().setTouchModeAbove(mode);
    }

}
